package com.hz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.hz.sys.filter.XssFilter;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import com.hz.sys.filter.AccessFilter;

import javax.servlet.http.HttpServletRequest;

@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient
@ServletComponentScan
public class WsApplication {
    private final Logger logger=LoggerFactory.getLogger(this.getClass());

	public static void main(String[] args) {
		new SpringApplicationBuilder(WsApplication.class).web(true).run(args);
	}

	@Bean
	public FilterRegistrationBean  filterRegistrationBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		AccessFilter accessFilter = new AccessFilter();
		registrationBean.setFilter(accessFilter);
		List<String> urlPatterns = new ArrayList<>(4);
		urlPatterns.add("/*");
		registrationBean.setUrlPatterns(urlPatterns);
		return registrationBean;
	}


     /**
      * @Author mcz
      * @Description  beetl模板配置
      * @Date 2018/9/29 16:25
      * @Param
      * @return
      **/
	@Bean(initMethod = "init", name = "beetlConfig")
	public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration(HttpServletRequest request) {
		BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = null;
		//HttpServletRequest request=WebUtil.getRequest();
		try {
			beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
			ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
			beetlGroupUtilConfiguration.setResourceLoader(classpathResourceLoader);
			//设置beetl全局变量
			Map<String,Object> shared = new HashMap<>();
			//String path = request.getContextPath();
			//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
			//shared.put("ctx", basePath);
			beetlGroupUtilConfiguration.setSharedVars(shared);
			//读取配置文件信息
//        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
		} catch (Exception e) {
			e.printStackTrace();
			//logger.error("*****************加载beetle模板异常{}",e.getMessage());
			//return null;
		}
		logger.info("***********************加载beetle模板成功！！！");
		return beetlGroupUtilConfiguration;
	}

	@Bean(name = "beetlViewResolver")
	public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setPrefix("/templates/");
		beetlSpringViewResolver.setSuffix(".html");
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
		beetlSpringViewResolver.setOrder(0);
		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
		return beetlSpringViewResolver;
	}

	/**
	 * xss过滤拦截器
	 */
	//@Bean
	public FilterRegistrationBean xssFilterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new XssFilter());
		filterRegistrationBean.setOrder(1);
		filterRegistrationBean.setEnabled(true);
		filterRegistrationBean.addUrlPatterns("/*");
		Map<String, String> initParameters = Maps.newHashMap();
		initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
		initParameters.put("isIncludeRichText", "true");
		filterRegistrationBean.setInitParameters(initParameters);
		return filterRegistrationBean;
	}



}
